/*
 * Copyright 2012 SerGenis. All rights reserved.
 */
package com.sergenis.form;

import java.util.Collections;
import java.util.Arrays;
import java.util.Set;

import com.sergenis.beans.AbstractBean;

/**
 * @author SerGenis
 * @deprecated 
 */
public class FormSetCollection<E>
    extends AbstractBean
    implements IDialogFormResultado {

    protected E active;
    public static final String PROP_ACTIVO = "active";

    public E getActive() {
	return active;
    }

    public void setActive(E active) {
	E oldActivo = this.active;
	this.active = active;
	firePropertyChange(PROP_ACTIVO, oldActivo, active);
    }
    protected Set<E> elements = Collections.EMPTY_SET;
    public static final String PROP_ELEMENTS = "elements";

    public Set getElements() {
	return elements;
    }

    public void setElements(Set<E> elements) {
	Set oldElements = this.elements;
	this.elements = elements;
	firePropertyChange(PROP_ELEMENTS, oldElements, elements);
    }

    public void add(E element) {
	E[] aElements = (E[]) elements.toArray();
	Set<E> newElements = new java.util.HashSet<E>(Arrays.asList(aElements));
	newElements.add(element);
	setElements(newElements);
    }

    public E get(int index) {
	E[] aElement = (E[]) elements.toArray();
	return aElement[index];
    }

    public int indexOf(Object o) {
	if (!elements.contains(o)) return -1;
	java.util.Iterator<E> itrt = elements.iterator();
	int index = 0;
	while (itrt.hasNext()) {
	}
	return index;
    }

    public E set(int index, E element) {
	E[] aElements = (E[]) elements.toArray();
	E oldElement = aElements[index];
	aElements[index] = element;
	Set<E> newElements = new java.util.HashSet<E>(Arrays.asList(aElements));
	setElements(newElements);
	return oldElement;
    }

    public boolean remove(E element) {
	E[] aElements = (E[]) elements.toArray();
	Set<E> newElements = new java.util.HashSet<E>(Arrays.asList(aElements));
	boolean remove = newElements.remove(element);
	if (remove) {
	    setElements(newElements);
	    return true;
	}
	return false;
    }

    public E remove(int index) {
	E[] aElements = (E[]) elements.toArray();
	E o = aElements[index];
	if (!elements.contains(o)) return null;
	remove(o);
	return o;
    }

    public int size() {
	return elements.size();
    }

    public boolean isEmpty() {
	return elements.isEmpty();
    }

    public void resetElements() {
	this.elements.clear();
	this.resultado = CANCELAR;
    }
    protected int resultado = CANCELAR;

    public void setResultado(int estado) {
	this.resultado = estado;
    }

    public int getResultado() {
	return resultado;
    }

    /**
     * Checks if the given index is in range.  If not, throws an appropriate
     * runtime exception.  This method does *not* check if the index is
     * negative: It is always used immediately prior to an array access,
     * which throws an ArrayIndexOutOfBoundsException if index is negative.
     */
    private void rangeCheck(int index) {
	if (index >= elements.size())
	    throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private String outOfBoundsMsg(int index) {
	return "Index: " + index + ", Size: " + elements.size();
    }
}
